package cn.xopencode.oss.dal.mysql.mapper.permission;

import cn.xopencode.oss.dal.mysql.dataobject.permission.PositionResourceDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public interface PositionResourceMapper extends BaseMapper<PositionResourceDO> {

    /**
     * 批量插入。因为 MyBaits Plus 的批量插入是基于 Service 实现，所以只好写 XML
     *
     * @param positionResources 数组
     */
    int insertList(@Param("positionResourceDOs") List<PositionResourceDO> positionResources);

    default List<PositionResourceDO> selectListByResourceId(String resourceId) {
        return selectList(new QueryWrapper<PositionResourceDO>().eq("resource_id", resourceId));
    }

    default List<PositionResourceDO> selectListByResourceIds(Collection<String> resourceIds) {
        return selectList(new QueryWrapper<PositionResourceDO>().in("resource_id", resourceIds));
    }

    default List<PositionResourceDO> selectListByPositionId(String positionId) {
        return selectList(new QueryWrapper<PositionResourceDO>().eq("position_id", positionId));
    }

    default List<PositionResourceDO> selectListByPositionIds(Collection<String> positionIds) {
        return selectList(new QueryWrapper<PositionResourceDO>().in("position_id", positionIds));
    }

    default int deleteByResourceId(String resourceId) {
        return delete(new QueryWrapper<PositionResourceDO>().eq("resource_id", resourceId));
    }

    default int deleteByPositionId(String positionId) {
        return delete(new QueryWrapper<PositionResourceDO>().eq("position_id", positionId));
    }

}
